﻿using System.Collections.Generic;
using System.Linq;
using RimWorld;
using RimWorld.Planet;
using Verse;
using Verse.Noise;

namespace ARMIR {
    /// <summary>
    /// 能力-抓握，在目标地区造成大范围的伤害，根据角色智识等级有所变化
    /// </summary>
    public class CompAbilityEffect_MindGrip : CompAbilityEffect {
        public new CompProperties_MindGrip Props => (CompProperties_MindGrip)props;

        public override void Apply(LocalTargetInfo target, LocalTargetInfo dest) {
            base.Apply(target, dest);
            float skillFactor = parent.pawn.skills.GetSkill(SkillDefOf.Intellectual).Level; //获取智识等级
            float explosiveTimes = skillFactor * 4 / 8 + 2; //技能伤害次数随智识0~20由2次到12次
            float explosiveRadius = Props.radius - skillFactor * 0.25f; //技能范围随智识0~20由10~5
            if (explosiveRadius < 5) { explosiveRadius = 5f; } 
            float thatAlotDamage = Props.damageAmount + skillFactor * 0.2f; //伤害量随智识0~20由x~x/2
            if (thatAlotDamage < Props.damageAmount / 2) { thatAlotDamage = Props.damageAmount / 2; }
            float armorPenetration = Props.armorPenetration + skillFactor * 0.25f;



            //foreach (IntVec3 cell in GenRadial.RadialCellsAround(target.Cell, Props.radius, true)) {
            //    if (cell.InBounds(parent.pawn.Map)) {
            //        List<Thing> thingsInCell = cell.GetThingList(parent.pawn.Map);

            //    }
            //}
            List<Thing> ExplosiveIgnoredPawns = new List<Thing>();
            ExplosiveIgnoredPawns = parent.pawn.Map.mapPawns.AllPawnsSpawned.Where(p => p.Faction == Faction.OfPlayer && p.Position.DistanceTo(target.Cell) < Props.radius + 2).OfType<Thing>().ToList();
            ExplosiveIgnoredPawns.AddRange(parent.pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.HaulableEver)).Where(p => p.Position.DistanceTo(target.Cell) < Props.radius + 2));
            ExplosiveIgnoredPawns.AddRange(parent.pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.Corpse)).Where(p => p.Position.DistanceTo(target.Cell) < Props.radius + 2));
            ExplosiveIgnoredPawns.Add(parent.pawn);

            int powerBase = (int)ARMIR_ModSettings.powerSet; 

            while (explosiveTimes > 0) {
                GenExplosion.DoExplosion(
                    target.Cell,
                    parent.pawn.Map,
                    radius: explosiveRadius,
                    DamageDefOf.Crush,
                    instigator: parent.pawn,
                    damAmount: (int)thatAlotDamage * powerBase,
                    armorPenetration: armorPenetration * powerBase,
                    damageFalloff: false, //伤害随距离衰减开启
                    screenShakeFactor: 0.5f, //屏幕震动强度(默认为1
                    explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_magicHitA"),
                    ignoredThings: ExplosiveIgnoredPawns
                );
                explosiveTimes = explosiveTimes - 1f;
            }

            
            bool hasEquipment = parent.pawn != null &&
            (parent.pawn.equipment.AllEquipmentListForReading.Any(e =>
            e.def == DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer")) ||
            parent.pawn.apparel.WornApparel.Any(a => a.def == DefDatabase<ThingDef>.GetNamed("ARMIR_FPAMindMixer")));

            if (hasEquipment) {
                parent.pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase<ThoughtDef>.GetNamed("ARMIR_MindLoad_weak")); //技能施放的思维负担
            }
            else {
                parent.pawn.needs.mood.thoughts.memories.TryGainMemory(DefDatabase<ThoughtDef>.GetNamed("ARMIR_MindLoad")); //技能施放的思维负担
            }

        }

        public override void DrawEffectPreview(LocalTargetInfo target) {
            float radius = Props.radius - parent.pawn.skills.GetSkill(SkillDefOf.Intellectual).Level * 0.25f;
            if (radius < 5) { radius = 5f; }
            GenDraw.DrawRadiusRing(target.Cell, radius);
        }
    }
}